
<?php
require_once 'models/models_baocao.php';
$obbc=new models_baocao();

if(!isset($data['listhd'])){?>
<link href="public/jquery/jquery.datepick.css"
	rel="stylesheet" type="text/css" />
<script
	type="text/javascript" src="public/jquery/jquery-1.4.4.js"></script>
<script
	type="text/javascript" src="public/jquery/jquery.datepick.js"></script>
<script
	type="text/javascript" src="public/jquery/jquery.datepick-vi.js"></script>
<script type="text/javascript">
$(function() {
	$('#popupDatepicker').datepick();
	$('#inlineDatepicker').datepick({onSelect: showDate});
	$('#Datepicker').datepick();
});

function showDate(date) {
	alert('The date chosen is ' + date);
}
</script>

<div id="title_right">
	<p id="title_right_left"></p>
	<div id="title_right_center">
		<p class="chu_title">LỢI NHUẬN</p>
	</div>
	<p id="title_right_right"></p>
</div>

<form name="doanhthu" method="post" action="">
	<table width="763" border="1" cellspacing="2" cellpadding="2"
		style="margin-top: 10px">
		<tr>
			<td align="center">Từ ngày:</td>
			<td><input type="text" name="tungay" id="popupDatepicker" /></td>
			<td align="center">Đến ngày:</td>
			<td><input type="text" name="denngay" id="Datepicker" /></td>
		</tr>
		<tr>
			<td colspan="4" align="right"><input type="submit" name="xem"
				id="xem" value="Xem" /></td>
		</tr>
	</table>
</form>
<?php
}
else{?>

<p align="center" style="margin: 10px; font-size: 25px">BÁO CÁO LỢI
	NHUẬN</p>
<p align="center" style="margin: 10px">
	Từ ngày:
	<?php echo $_POST['tungay'];?>
	đến ngày :
	<?php echo $_POST['denngay'];?>
</p>
<table class="table">
	<tr class="mau2">
		<td width="200px">Tên mặt hàng</td>
		<td width="80px">Giá nhập trung bình:</td>
		<td width="80px">Giá bán</td>
		<td>Số lượng bán</td>
		<td>Doanh số bán hàng</td>
		<td width="100px">Lợi nhuận</td>
		<td>%Lợi nhuận</td>
	</tr>
	<?php
	$tongloinhuan=0;
	$tongtien=0;
	$i=1;
	$a=0;
	//var_dump($data['listhd']);
	foreach ($data['listhd'] as $key => $hd){

		$data['listcthoadon']=$obbc->listcthoadon($hd['MaHD']);
		//var_dump($data['listcthoadon']);
		if(is_array($data['listcthoadon'])){
		foreach ($data['listcthoadon'] as $key =>$cthoadon){
				if(is_array($data['listmh'])){

					foreach ($data['listmh']  as $key =>$mh){

						if($cthoadon['MaMH']==$mh['MaMH']){

							$nhap=$obbc->listctn($mh['MaMH']);?>
							<tr>
								<td><?php echo $mh['TenMH'];?></td>
								<td><?php echo number_format($nhap['DonGiaNhap']);?></td>
								<td><?php echo number_format($cthoadon['DonGiaBan']);?></td>
								<td><?php echo $cthoadon['SoLuongBan'];?></td>
								<?php $thanhtien=$cthoadon['DonGiaBan']*$cthoadon['SoLuongBan'];?>
								<td><?php echo number_format($thanhtien);?></td>
								<?php $loinhuan=$thanhtien-($nhap['DonGiaNhap']*$cthoadon['SoLuongBan']);?>
								<td><?php echo number_format($loinhuan);?></td>
								<?php $ln=(($cthoadon['DonGiaBan']-$nhap['DonGiaNhap'])/$nhap['DonGiaNhap'])*100;?>
								<td><?php echo round($ln,2);?></td>
								<?php $tongtien+=$thanhtien;
								$tongloinhuan+=$loinhuan;
								?>
							</tr>

	<?php 							}
					}
				}
					
			}


		
		}
	}?>
	
	<tr>
		<td colspan="4" align="right">Tổng</td>
		<td><?php echo number_format($tongtien);?></td>
		<td><?php echo number_format($tongloinhuan);?></td>

	</tr>
</table>
	
<?php }?>